package com.lw.common.customview.attach_popup

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import coil.load
import com.chad.library.adapter4.BaseQuickAdapter
import com.chad.library.adapter4.viewholder.QuickViewHolder
import com.lxj.xpopup.R
import com.lxj.xpopup.core.BubbleAttachPopupView
import com.lxj.xpopup.util.XPopupUtils
import com.lxj.xpopup.widget.VerticalRecyclerView

@SuppressLint("ViewConstructor")
class CustomBubbleAttachPopup(context: Context, list: ArrayList<AttachBean>, selectListener: OnSelectListener?) :
    BubbleAttachPopupView(context) {
    var data: List<AttachBean> = list

    override fun getImplLayoutId(): Int {
        return R.layout._xpopup_attach_impl_list
    }

    override fun onCreate() {
        super.onCreate()
        setBubbleBgColor(Color.WHITE)
        setBubbleShadowSize(XPopupUtils.dp2px(context, 6f))
        setBubbleShadowColor(Color.GRAY)
        setArrowWidth(XPopupUtils.dp2px(context, 8f))
        setArrowHeight(XPopupUtils.dp2px(context, 9f))
        setArrowRadius(XPopupUtils.dp2px(context, 3f))
        val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
        recyclerView.setLayoutManager(LinearLayoutManager(context))

        val adapter: BaseQuickAdapter<AttachBean, QuickViewHolder> = object : BaseQuickAdapter<AttachBean, QuickViewHolder>() {
            override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): QuickViewHolder {
                return QuickViewHolder(R.layout._xpopup_adapter_text, parent)
            }

            override fun onBindViewHolder(holder: QuickViewHolder, position: Int, item: AttachBean?) {
                holder.setText(R.id.tv_text, item?.name)
                val imageView = holder.getViewOrNull<ImageView>(R.id.iv_image)
                if (item?.icon != null) {
                    if (imageView != null) {
                        XPopupUtils.setVisible(imageView, true)
                        if (item.icon == "") {
                            imageView.visibility = GONE
                        } else {
                            imageView.load(item.icon) {
                                error(com.lw.common.R.mipmap.img_erro)
                            }
                        }
                    }
                } else {
                    XPopupUtils.setVisible(imageView, false)
                }
            }
        }

        adapter.setOnItemClickListener { _, _, position ->
            if (selectListener != null) {
                selectListener!!.onSelect(position, adapter.getItem(position))
            }
            if (popupInfo.autoDismiss) dismiss()
        }

        recyclerView.setAdapter(adapter)
        adapter.submitList(data)
        (recyclerView as VerticalRecyclerView).setupDivider(popupInfo.isDarkTheme)
    }


    private var selectListener: OnSelectListener?

    init {
        this.selectListener = selectListener
        addInnerContent()
    }

    fun setOnSelectListener(selectListener: OnSelectListener?): CustomBubbleAttachPopup {
        this.selectListener = selectListener
        return this
    }

    interface OnSelectListener {
        fun onSelect(position: Int, item: AttachBean?)
    }
}
